<?php

class Lang
{

   var  $_languages = array();      //语言包队列

   var  $_lang_types = array();      //应用支持的语言类型数组

   var  $_lang_tp = '';      //应用支持的语言类型

 function __construct()
    {	
        $this->Lang();		
    }
	
	
 function Lang()
    {   
	    $this->_lang_tp = LANG;
		if(isset($GLOBALS['Lang']))
		{
		unset($GLOBALS['Lang']);		
		}

        $GLOBALS['Lang'] =& $this;
       
    }
   

 //产生数据库实体	
 function _get_lang($lang_name,$plug_dir = '')
    {
	   if (empty($this->_languages[$lang_name]))
       {   
	      $cookie_lang = get_cookie('languages');		
		  
	      if(empty($cookie_lang))
		  {	
		   $lang_type = $this->_lang_tp;		  	  
		  }
		  else
		  {
		   $lang_type = $cookie_lang;
		  }
		  
		  if(preg_match("/[.\/]/",$lang_type))
		  {
		   $lang_type = $this->_lang_tp;
		  
	      } 
		  
		  if(empty($lang_type))
		  {
		   $lang_type = $this->_lang_tp;
		  }
		  
		  $this->_get_lang_types();
		  
		  
		  if(!in_array($lang_type,$this->_lang_types))
		  {
		   $lang_type = $this->_lang_tp;		     
		  }

		  $this->_lang_tp = $lang_type;
		  $lang_dir = ROOT_PATH.'/languages/'.$lang_type.'/'.$lang_name.'.lang.php';
		  
		  if(!is_file($lang_dir))
		  {
		        if(empty($plug_dir))
				{
		        $lang_dir = ROOT_PATH .'/languages/'.$lang_type.'/'.$GLOBALS['SCup']->SCup_name.'/'.$lang_name.'.lang.php';				
				}
				else
				{
		        $lang_dir = ROOT_PATH .'/languages/'.$lang_type.'/plug/'.$plug_dir.'/'.$lang_name.'.lang.php';
				
				}
				
		  
		  }
		  		  
		  $this->_languages[$lang_name] = require($lang_dir);
		  if(isset($GLOBALS['SCup']->_get_view()->_var['lang']))
		  {	
          $GLOBALS['SCup']->_get_view()->_var['lang'] = array_merge($GLOBALS['SCup']->_get_view()->_var['lang'], $this->_languages[$lang_name]);
		  }
		  else
		  {
          $GLOBALS['SCup']->_get_view()->_assign('lang' , $this->_languages[$lang_name]);	
		  }
	  
	   }
	   
	   return $this->_languages[$lang_name];
	  	
    }	
	

 //得到languages目录下的所有文件夹及文件的名字
 function _get_lang_types()
    {
	   if ($this->_lang_types == null)
       {
	        $dirname = ROOT_PATH . '/languages';		
		    $this->_lang_types = get_all_dirname($dirname);
	   }
	   
	   return $this->_lang_types;
	  	
    }
	
	
 // 用户设置语言包
 function _set_lang($lang_type)
    {   
	   assign_cookie('languages',$lang_type);
	   
	   foreach($this->_languages as $key => $value)
	   {
	   unset($this->_languages[$key]);	 
	   $this->_get_lang($key);
	 
	   }	
       set_cookie('languages',$lang_type,time()+3600*24); 	   
	  	
    }	
	

  //得到语言包实体中的某个变量值采用点分"."数组方式,如$var=system.site_description则表示取$this->_languages[system][site_description]的值
 function _get_lang_var($var)
    {
	   $var = explode('.',trim($var));
	   if(empty($var[0])||empty($var[1]))
	   {
	    return false;
	   }
	
	   return $this->_languages[$var[0]][$var[1]];
    }




}




?>